local _G, canvas,tonumber, print
    = _G, canvas,tonumber, print

module 'audience_ui'

cam = 'no'
play = nil
users = 0

local dx, dy = canvas:attrSize()

local function draw_back (self)
   canvas:attrColor(0,0,0,self.alfa)
   canvas:drawRect('fill', 0,0, dx,dy)
end

local function draw_text (self)   
   canvas:attrFont('vera', self.size)
   canvas:attrColor('white')
   canvas:drawText(0,0, self.text)
end

local function draw_img (self, n, max)
   for i=1, tonumber(max) do
      x = (i-1)*17 + 42
      if i <= n then
         canvas:compose (x, 0, self.green)
      else
         canvas:compose (x, 0, self.gray)
      end
   end
end

local function draw_status (self)
   if cam == 'ok' then
      canvas:compose (0, 0, self.cam_ok)
   else
      canvas:compose (0, 0, self.cam_no)
   end
   if play == 'play' then
      canvas:compose (25, 3, self.play)
   elseif play then
      canvas:compose (25, 3, self.stop)
   end

end

people = {gray=canvas:new('img/gray_ps.png'), green=canvas:new('img/green_ps.png'), draw=draw_img }
status = {cam_ok=canvas:new('img/cam_ok.png'), cam_no=canvas:new('img/cam_no.png'), play=canvas:new('img/play.png'), stop=canvas:new('img/stop.png'), draw=draw_status }

background = {alfa = 100, draw=draw_back }

function set_status (val)
   cam = val
   redraw()
end

function set_play (val)
   play = val
   redraw()
end

function set_users (val)
   val = tonumber(val)
   users = val
   redraw()
end

function redraw()
	
        background:draw()
        status:draw()
        if cam == 'ok' then
           people:draw(0, users)
        end
	canvas:flush()
end

redraw()
